<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表单页面</title>
</head>
<body>
<h2>文件导入表单</h2>
<form id="uploadForm" action="excel/import" method="post" enctype="multipart/form-data">
    <!-- 下拉框 -->
    <label for="poolName">选择数据源:</label>
    <select id="poolName" name="poolName">
    </select>
    <br><br>

    <!-- 文件选择框 -->
    <label for="fileInput">选择文件:</label>
    <input type="file" id="fileInput" name="file" accept=".xls,.xlsx">
    <br><br>

    <!-- 提交按钮 -->
    <input type="submit" value="提交">
</form>

<script>
    // 可选：添加表单提交事件处理
    document.getElementById('uploadForm').addEventListener('submit', function(e) {
        e.preventDefault();
        const poolName = document.getElementById('poolName').value;
        const fileInput = document.getElementById('fileInput');

        if (!fileInput.files.length) {
            alert('请选择一个文件');
            return false;
        }

        const fileName = fileInput.files[0].name;
        const allowedExtensions = ['.xls', '.xlsx'];
        const fileExtension = fileName.substring(fileName.lastIndexOf('.')).toLowerCase();

        if (!allowedExtensions.includes(fileExtension)) {
            alert('请选择 .xls 或 .xlsx 格式的文件');
            return false;
        }

        const formData = new FormData();
        formData.append('poolName', poolName);
        formData.append('file', fileInput.files[0]);

        fetch('excel/import', {
            method: 'POST',
            body: formData
        })
            .then(response => response.json())
            .then(data => {
                if (data.code===1)
                {
                    console.log('数据源:', poolName);
                    console.log('文件:', fileInput.files[0]);
                    alert('表单已提交成功');
                }
                else
                {
                    alert('表单提交失败:' + data.msg);
                }
            })
            .catch(error => {
                console.error('提交失败:', error);
                alert('提交失败');
            });


    });

    // 在页面加载时获取分类数据
    document.addEventListener('DOMContentLoaded', function() {
        loadCategories();
    });

    // 加载分类数据
    function loadCategories() {
        fetch('/excel/Datasource')
            .then(response => response.json())
            .then(data => {
                const poolNameSelect = document.getElementById('poolName');
                poolNameSelect.innerHTML = ''; // 清空现有选项

                data.data.forEach(poolName => {
                    const option = document.createElement('option');
                    option.value = poolName;
                    option.textContent = poolName;
                    poolNameSelect.appendChild(option);
                });
            })
            .catch(error => {
                console.error('加载分类失败:', error);
            });
    }

</script>
</body>
</html>
